<?php

namespace app\index\dao;

use app\common\model\Transfer;
use ShidianUtil\Thinkphp\Dao\DaoTrait;
use think\facade\Config;

/**
 * TransferDao 数据访问对象
 * Class TransferDao
 * @package app\index\dao
 * @method Transfer findByPK(?int $id, $field = true)
 * @method \think\Collection selectAll(?int $page, ?int $page_size, $field = true)
 * @method Transfer insertActive(array $params, array $allow_field = [])
 * @method Transfer updateActive(Transfer $model, array $params, array $allow_field = [])
 * @method Transfer findAndUpdateActive(int $id, array $params, array $allow_field = [])
 * @method Transfer updateActiveByClosure(\Closure $closure, array $params, array $allow_field = [])
 * @method void deleteModel(Transfer $model)
 * @method void deleteById(int|array|\Closure $id, bool $force = false)
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037579
 * @link https://www.kancloud.cn/manual/think-orm/1257998
 * @link https://www.kancloud.cn/thinkphp/master-database-and-model/265551
 */
class TransferDao
{
    use DaoTrait;

    /**
     * @var Transfer
     */
    private $baseModel;

    /**
     * TransferDao constructor.
     */
    public function __construct()
    {
        $this->baseModel = Transfer::class;
    }

    /**
     * 插入转账记录
     * @param int $direction 0平台转出 1平台转入
     * @param string $transferType 转账业务类型
     * @param $memberType
     * @param $memberId
     * @param string $payMethod 转账账户类型
     * @param $accountNumber
     * @param $totalMoney
     * @param $describe
     * @param array $append
     * @param array $attach
     * @return Transfer
     */
    public function insertTransfer(int $direction, $transferType, $memberType, $memberId, $payMethod, $accountNumber, $totalMoney, $describe, array $append, array $attach = []): Transfer
    {

        $transferTypeClass = Config::get("transfer.transfers.$transferType.className", '');

        if (empty($attach)) {
            $attach['attach'] = 'attach';
        }

        // 插入转账申请表
        $params = [
            'direction' => $direction,
            'payMethod' => $payMethod,
            'accountNumber' => $accountNumber,
            'orderNo' => 'TS' . uuid(16),
            'describe' => $describe,
            'transferType' => $transferType,
            'transferTypeClass' => $transferTypeClass,
            'totalMoney' => $totalMoney,
            'memberType' => $memberType,
            'memberId' => $memberId,
            'transferStatus' => '0',
            'attach' => $attach,
        ];

        if (!empty($append)) {
            $params = array_merge($params, $append);
        }

        return $this->insertActive($params);
    }
}
